# Maintainer: Diego Sogari <diego.sogari@falker.com.br>

_realname=laszip
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.4.4
pkgrel=1
pkgdesc="Free and lossless LiDAR compression (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.laszip.org/"
msys2_repository_url="https://github.com/LASzip/LASzip"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(laszip-${pkgver}.tar.gz::https://github.com/LASzip/LASzip/releases/download/${pkgver}/laszip-src-${pkgver}.tar.gz
        "001-include-missing-header.patch"
        "laszip-3.1.1.patch"
        "laszip-3.4.3-mingw-utf8.patch")
sha256sums=('41f826848ff106f471b134224f58075ac8e36a62b6ba0ad75be8a8c191bdba4b'
            'df62cba1c6e358a9645be1f09d844d5d866431003884c243e13610132e169489'
            'c3d98f0bb85bb78e8cd31e1578799ed2ebda67b567a96c07d22b6eb2c9f208ef'
            '58cd2fd3507bdc6f8fca13982eae65599b23ad404df8629f6352942300ad1d11')
noextract=("laszip-${pkgver}.tar.gz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  echo "Extracting laszip-${pkgver}.tar.gz ..."
  tar -xzf laszip-${pkgver}.tar.gz || true

  cd "${srcdir}/${_realname}-src-${pkgver}.tar.gz"
  apply_patch_with_msg \
    001-include-missing-header.patch \
    laszip-3.1.1.patch \
    laszip-3.4.3-mingw-utf8.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    "../${_realname}-src-${pkgver}.tar.gz"

  cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .
}
